﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Pong_Project.Views;

namespace Pong_Project
{
    class Score : INotifyPropertyChanged
    {
        private static readonly Score singletonScore = new Score();

        private int scoreLeft = 0, scoreRight = 0, winningScore = 15;

        private InGame _signees;

        public event PropertyChangedEventHandler PropertyChanged;

        public static Score GetInstance()
        {
            return singletonScore;
        }

        public void AddSignee(InGame s)
        {
            _signees = s;
            PropertyChanged += s.WatchScore;
            s.WatchScore(this, new PropertyChangedEventArgs(""));

        }

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs args =
                    new PropertyChangedEventArgs(propertyName);
                PropertyChanged(this, args);
            }
        }


        public int ScoreLeft
        {
            get { return scoreLeft; }
            set
            {
                scoreLeft = value;
                OnPropertyChanged("scoreLeft");
                if (scoreLeft == winningScore)
                {
                    Pong_Project.Views.Winner.DeclareWinner(Players.Get(PlayerIdentity.PlayerLeft));
                    scoreLeft = 0;
                    scoreRight = 0;
                }
            }
        }

        public int ScoreRight
        {
            get { return scoreRight; }
            set
            {
                scoreRight = value;
                OnPropertyChanged("scoreRight");
                if (scoreRight == winningScore)
                {
                    Pong_Project.Views.Winner.DeclareWinner(Players.Get(PlayerIdentity.PlayerRight));
                    scoreLeft = 0;
                    scoreRight = 0;
                }
            }
        }

        public int WinningScore
        {
            get { return winningScore; }
            set { winningScore = value; OnPropertyChanged("winningScore"); }
        }

        internal void Reset()
        {
            scoreLeft = 0;
            scoreRight = 0;
        }
    }
}
